<template>
  <div v-if="logined == true" class="content">
    <div class="header">
      <div class="user">
        <img class="avatar" src="@/assets/images/avatar.png">
        <p class="user-name">{{ loginForm.username }}</p>
        <img class="right" src="@/assets/images/right.png">
      </div>
      <div class="info">
        <div class="histroy" @click="toHistroy()">
          <span class="histroy-count">{{ histroryCount }}</span>
          <span class="histroy-text">{{ '浏览历史' }}</span>
        </div>
        <div class="fav">
          <span class="fav-count">{{ favCount }}</span>
          <span class="fav-text">{{ '我的收藏' }}</span>
        </div>
      </div>
    </div>
    <div class="logout">
      <el-button
        size="medium"
        type="danger"
        style="width: 90%"
        @click.native.prevent="logout"
      >
        <span>退 出 登 录</span>
      </el-button>
    </div>
  </div>
  <div v-else>
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      label-position="left"
      label-width="0px"
      class="login-form"
    >
      <h2 class="title">欢迎使用</h2>
      <el-form-item prop="username">
        <el-input
          v-model="loginForm.username"
          type="text"
          auto-complete="off"
          placeholder="账号"
        >
          <svg-icon
            slot="prefix"
            icon-class="user"
            class="el-input__icon input-icon"
          />
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          type="password"
          auto-complete="off"
          placeholder="密码"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon
            slot="prefix"
            icon-class="password"
            class="el-input__icon input-icon"
          />
        </el-input>
      </el-form-item>
      <el-form-item style="">
        <slide-verify
          :l="42"
          :r="10"
          :w="350"
          :h="155"
          slider-text="向右滑动"
          @success="onSuccess"
          @fail="onFail"
        />
      </el-form-item>
      <el-form-item style="width: 100%">
        <el-button
          :disabled="!verified"
          :loading="loading"
          size="medium"
          type="danger"
          style="width: 100%"
          @click.native.prevent="handleLogin"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
      </el-form-item>

      <p class="register">
        <!-- <span class="memo">请使用Chrome,Firefox,IE 10+  </span> -->
        还没有帐号？
        <a href="/register" type="primary">立即注册</a>
      </p>
    </el-form>
  </div>
</template>

<script>

export default {
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', message: '用户名不能为空' }
        ],
        password: [
          { required: true, trigger: 'blur', message: '密码不能为空' }
        ]
      },
      loading: false,
      // 图形验证是否通过
      verified: false
    }
  },
  computed: {
    histroryCount() {
      return this.$store.state.my.histroy.length
    },
    favCount() {
      return this.$store.state.my.favourite.length
    },
    logined() {
      return this.$store.state.my.logined
    }
  },
  methods: {
    // 模拟登录成功
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true
          this.$store.commit('SET_LOGIN', true)
          this.loading = false
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 模拟注销登录
    logout() {
      this.$store.commit('SET_LOGIN', false)
      this.loginForm.username = ''
      this.loginForm.password = ''
      this.verified = false
    },
    toHistroy() {
      if (this.histroryCount > 0) {
        this.$router.push('/list')
      }
    },
    // 图形验证通过后，将登录按钮置为可用
    onSuccess() {
      this.verified = true
    }
  }
}

</script>

<style lang="scss"  scoped>
.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 100%;
  padding: 25px 25px 5px 25px;
  margin-top: 25px;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.title {
  margin: 0 auto 30px auto;
  text-align: center;
  color: #707070;
}

.register {
  float: right;
  font-size: 13px;
  // color: rgb(24, 144, 255);
}
a {
  color: #e72521;
  text-decoration: none;
  background-color: transparent;
  outline: none;
  cursor: pointer;
  transition: color 0.3s;
}
a:hover {
  color: #e72521;
}

.content {
  width: 100%;
  height: 100%;
  background-color: rgb(252, 248, 248);
}
.header {
  width: 100%;
  height: 5.33rem;
  background-color: #fff;
}

.user {
  margin-top: 0.5rem;
  overflow: hidden;
  padding: 0.5rem;
  height: 2.5rem;
  width: 100%;
}

.avatar {
  float: left;
  width: 1.8rem;
  height: 1.8rem;
  border-radius: 50%;
}

.user-name {
  float: left;
  margin-top: 0.6rem;
  margin-left: 0.5rem;
  color: #404040;
  font-size: 18px;
}

.right {
  float: right;
  width: 0.8rem;
  height: 0.8rem;
  margin-top: 0.6rem;
}

.info {
  float: left;
  padding: 1rem;
  height: 2.5rem;
  width: 100%;
}
.histroy {
  display: flex;
  float: left;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.histroy-count {
  color: #404040;
  font-size: 18px;
}

.histroy-text {
  margin-top: 0.1rem;
  color: #9b9191;
  font-size: 14px;
}

.fav {
  display: flex;
  float: right;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.fav-count {
  color: #404040;
  font-size: 18px;
}

.fav-text {
  margin-top: 0.1rem;
  color: #9b9191;
  font-size: 14px;
}

.logout {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 2rem;

}
</style>
